package com.next

class QuestionInstance implements Comparable
{

    String name
    String type
    String questionType
    Integer displayOrder = 0
    String value

    static belongsTo = [questionnaireInstance: QuestionnaireInstance]

    SortedSet options
    static hasMany = [options: OptionInstance]
    static mapping = {
        cache true
        options(lazy: false)
    }

    static constraints = {
        name maxSize: 64, unique: ['questionnaireInstance']
        type inList: ['fixed', 'random']
        questionType inList: ['input', 'radio', 'checkbox']
        value blank: true, nullable: true
    }

    @Override
    int compareTo(Object QuestionInstance)
    {

        def question = (QuestionInstance) QuestionInstance
        if (displayOrder == question.displayOrder)
        {
            return 0
        }
        else if (displayOrder > question.displayOrder)
        {
            return 1
        }
        else
        {
            return -1
        }
    }
}
